package com.weikai;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

public class SDcardBenchmark extends Activity {

    private TextView mText;
    private Button mButton;
    private EditText mEdit;
    private byte[] mBuf = new byte[10240];
    private ProgressBar mProgress;
    static final int DIALOG_RESULT = 0;
    private ProgressDialog mProgressDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mText = (TextView) findViewById(R.id.TextView01);
        mButton = (Button) findViewById(R.id.button_start);
        mButton.setOnClickListener(mClickListener);
        mEdit = (EditText) findViewById(R.id.EditText01);
    }

    private long timingRead(int size) {
        long t = SystemClock.uptimeMillis();
        return SystemClock.uptimeMillis() - t;
    }

    private long timingWrite(int size) {
        long t = 0;
        try {
            FileOutputStream fo = new FileOutputStream("/sdcard/junkdata");
            t = SystemClock.uptimeMillis();
            for (int i = 0; i < 1000; i++)
                fo.write(mBuf, 0, size);
            fo.flush();
            t = SystemClock.uptimeMillis() - t;
            fo.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return t;
    }

    private OnClickListener mClickListener = new OnClickListener() {
        public void onClick(View v) {
            int s = Integer.valueOf(mEdit.getText().toString());
            new BenchmarkTask().execute(s);
        }
    };

    // protected Dialog onCreateDialog(int id) {
    // Dialog dialog;
    // switch(id) {
    // case DIALOG_RESULT:
    // break;
    // default:
    // dialog = null;
    // }
    // return dialog;
    // }
    private class BenchmarkTask extends AsyncTask<Integer, Integer, Float> {
        protected Float doInBackground(Integer... size) {
            return (size[0].floatValue() / timingWrite(size[0]) * 1000);
        }

        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(SDcardBenchmark.this, "",
                    "Bechmarking. Please wait...", true);
        }

        protected void onPostExecute(Float result) {
            mProgressDialog.dismiss();
            mText.setText("SD card can be writen at " + result + " kilobytes/second");
        }
    }
}